package com.sloth.bear.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sloth.bear.common.ResultCode;
import com.sloth.bear.dto.entity.DocItem;
import com.sloth.bear.dto.request.DocumentItemRequest;
import com.sloth.bear.dto.vo.DocumentItemVo;
import com.sloth.bear.exception.BusinessException;
import com.sloth.bear.mapper.DocumentItemMapper;
import com.sloth.bear.mapper.DocumentMapper;
import com.sloth.bear.service.DocItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 文档子项服务实现类
 */
@RequiredArgsConstructor
@Service
public class DocItemServiceImpl implements DocItemService {

    private final DocumentItemMapper documentItemMapper;
    private final DocumentMapper documentMapper;

    @Override
    public List<DocumentItemVo> getItemsByDocumentId(Long documentId) {
        LambdaQueryWrapper<DocItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocItem::getDocumentId, documentId)
               .orderByAsc(DocItem::getSortNo);
        
        List<DocItem> items = documentItemMapper.selectList(wrapper);
        return items.stream()
                   .map(this::convertToVo)
                   .collect(Collectors.toList());
    }

    @Override
    public DocumentItemVo getItemById(Long id) {
        DocItem item = documentItemMapper.selectById(id);
        if (item == null) {
            throw new BusinessException(ResultCode.BAD_REQUEST.getCode(), "文档子项不存在");
        }
        return convertToVo(item);
    }

    @Override
    public DocumentItemVo createItem(DocumentItemRequest request) {
        // 验证文档是否存在
        if (documentMapper.selectById(request.getDocumentId()) == null) {
            throw new BusinessException(ResultCode.BAD_REQUEST.getCode(), "关联的文档不存在");
        }

        DocItem item = new DocItem();
        item.setDocumentId(request.getDocumentId());
        item.setName(request.getName());
        item.setContent(request.getContent());
        item.setSortNo(request.getSortNo() != null ? request.getSortNo() : 0);

        int result = documentItemMapper.insert(item);
        if (result <= 0) {
            throw new BusinessException(ResultCode.BAD_REQUEST.getCode(), "创建文档子项失败");
        }
        return convertToVo(item);
    }

    @Override
    public DocumentItemVo updateItem(Long id, DocumentItemRequest request) {
        DocItem existItem = documentItemMapper.selectById(id);
        if (existItem == null) {
            throw new BusinessException(ResultCode.BAD_REQUEST.getCode(), "文档子项不存在");
        }

        // 验证文档是否存在
        if (documentMapper.selectById(request.getDocumentId()) == null) {
            throw new BusinessException(ResultCode.BAD_REQUEST.getCode(), "关联的文档不存在");
        }

        existItem.setDocumentId(request.getDocumentId());
        existItem.setName(request.getName());
        existItem.setContent(request.getContent());
        existItem.setSortNo(request.getSortNo() != null ? request.getSortNo() : 0);

        int result = documentItemMapper.updateById(existItem);
        if (result <= 0) {
            throw new BusinessException(ResultCode.BAD_REQUEST.getCode(), "更新文档子项失败");
        }
        return convertToVo(existItem);
    }

    @Override
    public boolean deleteItem(Long id) {
        if (id == null) {
            throw new BusinessException(ResultCode.BAD_REQUEST.getCode(), "文档子项ID不能为空");
        }

        DocItem existItem = documentItemMapper.selectById(id);
        if (existItem == null) {
            throw new BusinessException(ResultCode.BAD_REQUEST.getCode(), "文档子项不存在");
        }

        int result = documentItemMapper.deleteById(id);
        if (result <= 0) {
            throw new BusinessException(ResultCode.BAD_REQUEST.getCode(), "删除文档子项失败");
        }
        return true;
    }

    private DocumentItemVo convertToVo(DocItem item) {
        DocumentItemVo vo = new DocumentItemVo();
        BeanUtils.copyProperties(item, vo);
        return vo;
    }
}